﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Xml;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    internal class EnvironmentalProfileViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string mName = null;
        private double mValue = 0.0;
        public string Name
        {
            get { return mName; }
            set
            {
                mName = value;
                RaisePropertyChanged("Name");
            }
        }
        public double Value
        {
            get { return mValue; }
            set
            {
                mValue = value;
                RaisePropertyChanged("Value");
            }
        }

        public XmlElement Export(XmlDocument doc)
        {
            XmlElement _element = null;
            if (null == doc) return _element;
            if (string.IsNullOrEmpty(this.Name)) return _element;

            _element = doc.CreateElement("EnvironmentalProfile");

            XmlAttribute _name = doc.CreateAttribute("Name");
            _name.Value = this.Name;
            _element.Attributes.Append(_name);

            XmlAttribute _value = doc.CreateAttribute("Value");
            _value.Value = this.Value.ToString();
            _element.Attributes.Append(_value);

            return _element;
        }

        public void Import(XmlElement element)
        {
            if (null == element) return;
            if ("EnvironmentalProfile" == element.Name)
            {
                if (element.HasAttribute("Name")) this.Name = element.Attributes["Name"].Value;
                if (element.HasAttribute("Value")) this.Value = double.Parse(element.Attributes["Value"].Value);
            }
        }

        private void RaisePropertyChanged(string strPropertyName)
        {
            PropertyChangedEventHandler _handler = this.PropertyChanged;
            if (null != _handler) _handler(this, new PropertyChangedEventArgs(strPropertyName));
        }
    }
}
